package com.chivan.encoder.recorder;

import android.content.Context;
import android.media.AudioManager;

/**
 * 音频播放的焦点获取与归还
 */
public class AudioFocusUtil {
    public static boolean requestAudioFocus(Context context) {
        AudioManager mAudioManager =
                (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        //mFocusChangeListener只监听失去焦点或者再次获得焦点时的事件
        //本次申请事件是不被mAudioFocusListener处理的
        int result = mAudioManager.requestAudioFocus(null,
                AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

        return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == result;
    }

    public static boolean abandonAudioFocus(Context context) {
        AudioManager mAudioManager =
                (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
                mAudioManager.abandonAudioFocus(null);
    }
}